package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;

import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public List<ProductModel> findAll(ProductModel model) {
        String sql = "SELECT p.*,c.name typeName FROM product p left join clothing_type c on p.type=c.sort  ";
        sql += "where 1=1  ";
        if (!("".equals(model.getName()) || (model.getName() == null))) {
            sql += " and p.name like '%" + model.getName() + "%'";
        }
        sql += " order by id desc";
        sql += " limit " + (model.getPageNo() - 1) * model.getPageSize() + ", " + model.getPageSize() + "";
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
                productModel.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                productModel.setMarketPrice(resultSet.getBigDecimal("price"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setIsShow(resultSet.getInt("is_show"));
                productModel.setIsRecommend(resultSet.getInt("is_recommend"));
                productModel.setTypeName(resultSet.getString("typeName"));
                productModel.setUpdateTime(resultSet.getString("update_time"));
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int getCount(ProductModel productModel) {
        String sql = "SELECT count(*) count  FROM product p\n" +
                "left join clothing_type c\n" +
                "on p.type=c.sort ";
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int add(ProductModel model) {
        String sql = "INSERT INTO `product` (`name`, " +
                "`normal_price`, `price`, " +
                "`content`, `is_show`, `is_recommend`, " +
                "`img_href`, `create_time`, `update_time`," +
                " `remark`, `enable`, " +
                "`where_show`, `cuisine_id`) VALUES (?, " +
                "?,?, " +
                "?, ?, ?," +
                " ?,now(), now()," +
                " '', '1'," +
                " '1', ?);\n";
        List list = new ArrayList();
        list.add(model.getName());
        list.add(model.getNormalPrice());
        list.add(model.getMarketPrice());
        list.add(model.getContent());
        list.add(model.getIsShow());
        list.add(model.getIsRecommend());
        list.add(model.getImgHref());
        list.add(model.getCuisineId());
        return JDBC.updatea(sql, list);
    }

    @Override
    public int update(ProductModel model) {
        return 0;
    }

    @Override
    public ProductModel findById(ProductModel productModel) {
        String sql = "select p.*,c.name typeName from product p left join clothing_type c on p.type=c.sort \n" +
                " where p.id =" + productModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        ProductModel model = null;
        try {
            while (resultSet.next()) {
                model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setTypeName(resultSet.getString("typeName"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setIsRecommend(resultSet.getInt("is_recommend"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setContent(resultSet.getString("content"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int delete(ProductModel model) {
        String sql = "delete from product where id =" + model.getId();
        return JDBC.update(sql);
    }

    private String setSql(ProductModel model) {
        String sql = "";
        if (!"".equals(model.getName())) {
            sql += " and p.name like '%" + model.getName() + "%'";
        }
        return sql;
    }

}
